package com.hb.trans.mvp.ui.activity


import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.basiclib.utils.ImmersionBar
import com.ctcnit.templatepro.global.MyGlideModule
import com.hb.trans.R
import com.hb.trans.bean.Coach
import com.hb.trans.global.ACTIVITY_SCHOOL_INFO_COACH_INFO
import com.hb.trans.mvp.contract.CoachInfoContract
import com.hb.trans.mvp.presenter.CoachInfoPresenter
import com.hb.trans.mvp.ui.adapter.EvaluationAdapter
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_coach_info.*
import javax.inject.Inject

@Route(path = ACTIVITY_SCHOOL_INFO_COACH_INFO)
class CoachInfoActivity : BaseActivity(), CoachInfoContract.View {


    @Inject
    lateinit var mPresenter: CoachInfoPresenter
    @Inject
    lateinit var mAdapter: EvaluationAdapter

    @JvmField
    @Autowired
    var coachInfo: Coach? = null

    override fun getContentViewResId(): Int = R.layout.activity_coach_info

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        mPresenter.attachView(this)
        ImmersionBar.with(this)
            .statusBarColor(R.color.colorPrimary)
            .init()
    }

    override fun getPresenter(): CoachInfoPresenter? = mPresenter

    override fun initView() {
        setSupportActionBar(coachInfoToolbar.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.mipmap.ic_left_arrow)

        rvEvaluate.apply {
            layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this@CoachInfoActivity)
            adapter = mAdapter
        }
        coachInfo?.let {
            MyGlideModule.load(this, it.photourl, ivCoachAvater)
            tvCoachName.text = it.name
            tvCarType.text = it.teachpermitted
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return true
    }

    override fun setListener() {
//        coachInfoToolbar.backClick()?.subscribe { finish() }
    }

    override fun initData() {
        coachInfo?.let {
            mPresenter.getCoachInfo(it.coanum)
        }
    }

    override fun setupCoachInfo(coach: Coach?) {
        coach?.let {
            tvEvaluationCount.text = "学员评价(${it.pjList?.size ?: 0})"
            tvCount.text = spanText("带教人数 ${it.tstunum}", R.color.gray_66, 0, 4)
            tvYield.text =
                spanText("学生合格率 ${it.passPersent?.toFloatOrNull()?.times(100) ?: "0"}%", R.color.gray_66, 0, 5)
            mAdapter.setNewData(coach.pjList)
        }
    }

    private fun spanText(text: String, foreColorId: Int, start: Int, end: Int): SpannableString {
        val spannableString = SpannableString(text)
        val foreColor = ContextCompat.getColor(this, foreColorId)
        val colorSpan = ForegroundColorSpan(foreColor)
        spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        return spannableString
    }

    override fun onDestroy() {
        super.onDestroy()
        ImmersionBar.with(this).destroy()
    }
}
